def is_correct(hmax, mmax, time):
h, m = time.split(":")
mirrored = {"0": "0", "1":"1", "2": "5", "3": "",
"4": "", "5": "2", "6": "", "7": "", "8": "8", "9": ""}
mirrored_h = mirrored[h[1]]+mirrored[h[0]]
mirrored_m = mirrored[m[1]]+mirrored[m[0]]
if len(mirrored_h) == 2 and int(mirrored_h) < mmax \
and len(mirrored_m) == 2 and int(mirrored_m) < hmax:
return mirrored_m+":"+mirrored_h
return False
def main():
t = int(input())
for _ in range(t):
hmax, mmax = map(int, input().split())
hnow, mnow = map(int, input().split(":"))
result = "00:00"
end = False
for hour in range(hnow, hmax):
start = mnow if hour == hnow else 0
for minute in range(start, mmax):
time = str(hour).rjust(2, "0")+":"+str(minute).rjust(2, "0")
mirrored = is_correct(hmax, mmax, time)
if mirrored:
result = time
end = True
break
if end: break
print(result)
main()
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL);
const int mod = 1000000007;
const ll M = 998244353;
ll mul(ll a, ll b){
return ((a%M)*(b%M))%M;
}
ll modPower(ll a, ll b){
if(b==0){
return 1;
}
a = a%M;
ll res=1;
while(b>0){
if(b&1){
res = mul(res, a);
}
b = b>>1;
a = mul(a, a);
}
return res;
}
ll fac(ll n){
ll res=1;
for(ll i=2; i<=n; i++){
res = mul(res, i);
}
return res;
}
ll dv(ll a, ll b){
return mul(a, modPower(b, M-2));
}
ll nCr(ll n, ll r){
return dv(fac(n), mul(fac(r), fac(n-r)));
}
vi v = {0, 1, 5, -1, -1, 2, -1, -1, 8, -1};
int INF = 1e9+7;
int fun(int x) {
string s = to_string(x);
if(x<10){
s="0"+s;
}
string res="";
for(int i=1; i>=0; i--){
if (v[s[i]-'0']==-1){
return INF;
}
res += char(v[s[i]-'0'] + '0');
}
return stoi(res);
}
string good(int n){
string s=to_string(n);
if(n<10){
s = "0"+s;
}
return s;
}
void solve(){
int h, m;
cin>>h>>m;
string s;
cin>>s;
int a=(s[0]-'0')*10+(s[1]-'0');
int b=(s[3]-'0')*10+(s[4]-'0');
while(true){
a+=(b/m);
b%=m;
a%=h;
if(fun(a)<m && fun(b)<h){
cout<<good(a)<<":"<<good(b)<<'\n';
return;
}
b++;
}
}
int main(){
fast_io
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}
1144A - Diverse Strings | 1553B - Reverse String |
1073A - Diverse Substring | 630N - Forecast |
312B - Archer | 34D - Road Map |
630I - Parking Lot | 160B - Unlucky Ticket |
371B - Fox Dividing Cheese | 584B - Kolya and Tanya |
137B - Permutation | 550C - Divisibility by Eight |
5A - Chat Servers Outgoing Traffic | 615A - Bulbs |
5B - Center Alignment | 549A - Face Detection |
535B - Tavas and SaDDas | 722C - Destroying Array |
366A - Dima and Guards | 716B - Complete the Word |
1461C - Random Events | 1627A - Not Shading |
141B - Hopscotch | 47B - Coins |
1466C - Canine poetry | 74A - Room Leader |
1333D - Challenges in school №41 | 1475B - New Year's Number |
461A - Appleman and Toastman | 320B - Ping-Pong (Easy Version) |